﻿using Microsoft.Win32;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Install
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private static RichTextBox richText = new RichTextBox();
        private static FlowDocument richDoc = new FlowDocument();
        internal static MainWindow instance = null;
        internal bool Doing = false;
        
        public MainWindow()
        {
            InitializeComponent();
            instance = this;
            this.ChoicePath();
        }

        private void ChoicePath()
        {
            this.Title = "司法鉴定管理程序 - 安装";
            this.grid0.Children.Clear();
            Label label1 = new Label();
            label1.FontSize = 28;
            label1.Content = "设置程序安装目录：";
            label1.Margin = new Thickness(5, 30, 0, 0);
            grid0.Children.Add(label1);
            TextBox tb1 = new TextBox();
            tb1.TextWrapping = TextWrapping.WrapWithOverflow;
            tb1.MinHeight = 32;
            tb1.HorizontalAlignment = HorizontalAlignment.Stretch;
            tb1.VerticalAlignment = VerticalAlignment.Top;
            tb1.Margin = new Thickness(10, 110, 10, 0);
            tb1.FontSize = 16;
            tb1.Text = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\SFJDManagement";
            tb1.IsEnabled = false;
            tb1.TextChanged += Tb1_TextChanged;
            grid0.Children.Add(tb1);
            Button button1 = new Button();
            button1.Content = "自定义路径";
            button1.FontSize = 14;
            button1.Width = 80;
            button1.Height = 28;
            button1.HorizontalAlignment = HorizontalAlignment.Right;
            button1.VerticalAlignment = VerticalAlignment.Top;
            button1.Margin = new Thickness(0, 75, 10, 0);
            button1.Click += Button1_Click;
            grid0.Children.Add(button1);

            DockPanel dockPanel = new DockPanel()
            {
                Height = 50,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Bottom,
                Margin = new Thickness(0, 0, 0, 20)
            };
            grid0.Children.Add(dockPanel);
            RegistryHelper reg = new RegistryHelper();
            if (reg.IsRegistryExist(Registry.CurrentUser, "SOFTWARE", "SFJDManagement"))
            {
                Button button_Uninstall = new Button()
                {
                    Content = "卸载",
                    FontSize = 20,
                    Width = 100,
                    Height = 50,
                    Margin = new Thickness(10 ,0, 10, 0),
                };
                button_Uninstall.Click += Button_Uninstall_Click;
                dockPanel.Children.Add(button_Uninstall);
            }
            else
            {
                Button button_Install = new Button()
                {
                    Content = "开始安装",
                    FontSize = 20,
                    Width = 100,
                    Height = 50,
                    Margin = new Thickness(10, 0, 10, 0),
                };
                button_Install.Click += Button_Install_Click;
                dockPanel.Children.Add(button_Install);
            }
            Button button_Repair = new Button()
            {
                Content = "修复",
                FontSize = 20,
                Width = 100,
                Height = 50,
                Margin = new Thickness(10, 0, 10, 0),
            };
            button_Repair.Click += Button_repair_Click;
            dockPanel.Children.Add(button_Repair);
        }

        private void Button_repair_Click(object sender, RoutedEventArgs e)
        {
            string path = ((TextBox)grid0.Children[1]).Text;
            ShowBox();
            new ProgramUnintall();
            new ProgramInstall(path, 2);
        }

        private void Button_Uninstall_Click(object sender, RoutedEventArgs e)
        {
            ShowBox();
            new ProgramUnintall();
        }

        private void Button_Install_Click(object sender, RoutedEventArgs e)
        {
            string path = ((TextBox)grid0.Children[1]).Text;
            ShowBox();
            new ProgramInstall(path, 1);
        }

        private void ShowBox()
        {
            grid0.Children.Clear();
            grid0.Children.Add(richText);
            richText.IsReadOnly = true;
            richText.HorizontalAlignment = HorizontalAlignment.Stretch;
            richText.VerticalAlignment = VerticalAlignment.Stretch;
            richText.Margin = new Thickness(10, 10, 10, 50);
            richText.TextChanged += RichText_TextChanged;

            Button btn_Finish = new Button()
            {
                Content = "完成",
                FontSize = 20,
                Width = 200,
                Height = 50,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Bottom
            };
            btn_Finish.Click += Btn_Finish_Click;
            grid0.Children.Add(btn_Finish);
        }

        private void RichText_TextChanged(object sender, TextChangedEventArgs e)
        {
            richText.ScrollToEnd();
        }

        private void Btn_Finish_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
            Dispatcher.InvokeShutdown();
        }

        private void Tb1_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textbox = (TextBox)sender;
            if (sender.ToString().IndexOf("SFJDM") < 0)
            {
                textbox.Text += "\\SFJDManagement";
            }
        }
        
        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog();
            //folder.RootFolder = Environment.SpecialFolder.ProgramFiles;
            folder.ShowNewFolderButton = false;
            //folder.ShowDialog();
            if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ((TextBox)grid0.Children[1]).Text = folder.SelectedPath;
            }
        }

        public void OutputMsg(string msg)
        {
            Dispatcher.Invoke(new Action(delegate
            {
                var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签  
                var r = new Run(msg); // Run 是一个 Inline 的标签
                p.Inlines.Add(r);
                p.LineHeight = 1;
                p.Foreground = Brushes.Black;//设置字体颜色  
                richDoc.Blocks.Add(p);
                richText.Document = richDoc;
                richText.Focus();
            }));
        }

        public void OutputErr(string msg)
        {
            Dispatcher.Invoke(new Action(delegate
            {
                var p = new Paragraph(); // Paragraph 类似于 html 的 P 标签
                var r = new Run(msg); // Run 是一个 Inline 的标签
                p.Inlines.Add(r);
                p.LineHeight = 1;
                p.Foreground = Brushes.Red;//设置字体颜色
                richDoc.Blocks.Add(p);
                richText.Document = richDoc;
            }));
        }
    }
}
